﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 4th, 2013
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
02.04.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using GameJolt.Async;
using GameJolt.Requests;
using Newtonsoft.Json;
#endregion

namespace GameJolt
{
	/// <summary>
	/// 
	/// </summary>
	[JsonObject]
	public class User
	{
		#region Properties
		[JsonProperty("id")]
		public string ID
		{
			get;
			set;
		}

		[JsonProperty("type")]
		public string Type
		{
			get;
			set;
		}

		[JsonProperty("username")]
		public string Username
		{
			get;
			set;
		}

		[JsonProperty("avatar_url")]
		public string AvatarUrl
		{
			get;
			set;
		}

		[JsonProperty("signed_up")]
		public string SignedUp
		{
			get;
			set;
		}

		[JsonProperty("last_logged_in")]
		public string LastLoggedIn
		{
			get;
			set;
		}

		[JsonProperty("status")]
		public string Status
		{
			get;
			set;
		}

		[JsonProperty("developer_name")]
		public string DeveloperName
		{
			get;
			set;
		}

		[JsonProperty("developer_website")]
		public string DeveloperWebsite
		{
			get;
			set;
		}

		[JsonProperty("developer_description")]
		public string DeveloperDescription
		{
			get;
			set;
		}
		#endregion

		#region Methods
		public static IAsyncResult BeginGetByUsername(string gameID, string privateKey, string username, AsyncCallback callback = null, object asyncState = null)
		{
			var request = UserRequestFactory.CreateGetRequest(gameID, privateKey, username);
			return request.Begin(callback, asyncState);
		}

		public static User EndGetByUsername(IAsyncResult result)
		{
			var joltResult = (AsyncResult<User[]>)result;
			var request = (JsonRequest<User[], UserRequestFactory.UserResult>)joltResult.CoreData;
			var results = request.End(result);
			if(results == null || results.Length == 0)
				return null;
			return results[0];
		}

		public static User GetByUsername(string gameID, string privateKey, string username)
		{
			var request = UserRequestFactory.CreateGetRequest(gameID, privateKey, username);
			var results = request.Process(null);
			if(results == null || results.Length == 0)
				return null;
			return results[0];
		}

		public static IAsyncResult GetByUsername(string gameID, string privateKey, string username, Action<User> callback)
		{
			return BeginGetByUsername(gameID, privateKey, username, (result) => callback(EndGetByUsername(result)));
		}

		public static IAsyncResult BeginGetByUserIDs(string gameID, string privateKey, string[] userIDs, AsyncCallback callback = null, object asyncState = null)
		{
			var request = UserRequestFactory.CreateGetRequest(gameID, privateKey, userIDs);
			return request.Begin(callback, asyncState);
		}

		public static User[] EndGetByUserIDs(IAsyncResult result)
		{
			var joltResult = (AsyncResult<User[]>)result;
			var request = (JsonRequest<User[], UserRequestFactory.UserResult>)joltResult.CoreData;
			return request.End(result);
		}

		public static User[] GetByUserIDs(string gameID, string privateKey, params string[] userIDs)
		{
			var request = UserRequestFactory.CreateGetRequest(gameID, privateKey, userIDs);
			return request.Process(null);
		}

		public static IAsyncResult GeByUserIDs(string gameID, string privateKey, string[] userIDs, Action<User[]> callback)
		{
			return BeginGetByUserIDs(gameID, privateKey, userIDs, (result) => callback(EndGetByUserIDs(result)));
		}

		public static IAsyncResult BeginAuthenticateUser(string gameID, string privateKey, string username, string userToken, AsyncCallback callback = null, object asyncState = null)
		{
			var request = UserRequestFactory.CreateAuthRequest(gameID, privateKey, username, userToken);
			return request.Begin(callback, asyncState);
		}

		public static string EndAuthenticateUser(IAsyncResult result)
		{
			var joltResult = (AsyncResult<string>)result;
			var request = (StringRequest)joltResult.CoreData;
			return request.End(result);
		}

		public static string AuthenticateUser(string gameID, string privateKey, string username, string userToken)
		{
			var request = UserRequestFactory.CreateAuthRequest(gameID, privateKey, username, userToken);
			return request.Process(null);
		}

		public static IAsyncResult AuthenticateUser(string gameID, string privateKey, string username, string userToken, Action<string> callback)
		{
			return BeginAuthenticateUser(gameID, privateKey, username, userToken, (result) => callback(EndAuthenticateUser(result)));
		}
		#endregion
	}
}
